#include "Character.hpp"

Character::Character():CharacterEntity(), ServerEntity(){
	facingDir = Entity::dir_N;
	type = 0;
}

void Character::bind(lua_State* luaState){

	luabind::module(luaState)[
        luabind::class_<Character>("CharacterEntity")
			.def_readonly("index", &Character::index)
			.def_readonly("type", &Character::type)
			.def_readwrite("x", &Character::x)
            .def_readwrite("y", &Character::y)
			.def_readwrite("width", &Character::cWidth)
			.def_readwrite("height", &Character::cHeight)
			.def_readwrite("xOffset", &Character::cxOffset)
			.def_readwrite("yOffset", &Character::cyOffset)
			.def_readwrite("maxVelocityX", &Character::maxVelocityX)
			.def_readwrite("maxVelocityY", &Character::maxVelocityY)
			.def_readwrite("acceleration", &Character::acceleration)
            .def_readwrite("hasScript", &Character::hasScript)
            .def_readwrite("updateable", &Character::updateable)
			.def("setMove", &Character::setMove)
            .def("setCharacterSet", &Character::setCharacterSet)
			.enum_("constant")[
				luabind::value("North", Entity::dir_N),
				luabind::value("East", Entity::dir_E),
				luabind::value("South", Entity::dir_S),
				luabind::value("West", Entity::dir_W)
			]
    ];
}

void Character::push(lua_State* luaState){
	luabind::object o = luabind::object(luaState, this);
    o.push(luaState);
}

void Character::setCharacterSet(const char* name){
    characterSet = GameDrawer::Instance()->spriteManager.getSpriteSet(name);
}

void Character::draw(sf::RenderTarget& target, sf::RenderStates states) const{
    if(!characterSet)
        return;
    
    states.transform *= sf::Transformable::getTransform();
	
    if(moving){
        if(direction&Entity::dir_N)
            target.draw(*characterSet->sprites[0], states);
        else if(direction&Entity::dir_E)
            target.draw(*characterSet->sprites[1], states);
        else if(direction&Entity::dir_S)
            target.draw(*characterSet->sprites[2], states);
        else if(direction&Entity::dir_W)
            target.draw(*characterSet->sprites[3], states);
    }else{
        if(facingDir&Entity::dir_N)
            target.draw(*characterSet->sprites[4], states);
        else if(facingDir&Entity::dir_E)
            target.draw(*characterSet->sprites[5], states);
        else if(facingDir&Entity::dir_S)
            target.draw(*characterSet->sprites[6], states);
        else if(facingDir&Entity::dir_W)
            target.draw(*characterSet->sprites[7], states);
    }
	sf::RectangleShape collisionBox;
	collisionBox.setOutlineThickness(1.0);
	collisionBox.setFillColor(sf::Color::Transparent);
	collisionBox.setOutlineColor(sf::Color::Magenta);
	collisionBox.setSize(sf::Vector2f(cWidth, cHeight));
    states.transform = states.transform.translate(cxOffset, cyOffset);
	target.draw(collisionBox, states);
}

void Character::move(){
    CharacterEntity::move();
}

void Character::setMove(byte dir){
    if(dir)
        facingDir = dir;

    Entity::setMove(dir);
}

void Character::update(){
}